package com.project.lk.nurseapp.common.service;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseApp;
import com.project.lk.nurseapp.db.RemindBean;
import com.project.lk.nurseapp.utils.RemindInfoUtils;
import com.project.lk.nurseapp.utils.TimeUtils;

import java.util.Calendar;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class AlarmReceiver extends BroadcastReceiver {
    private NotificationManager m_notificationMgr = null;
    private static final int NOTIFICATION_FLAG = 3;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("MLOG","闹钟。。。。");
        m_notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (intent.getAction().equals("alarm_receiver")) {
            String alarmId = intent.getStringExtra("alarmId");
            RemindBean remindBean = RemindInfoUtils.getUserInfoFromCache(Integer.parseInt(alarmId)+1 + "");
            if (remindBean.getIsOpen()){
                String[] split = remindBean.getTime().split(":");
                String remindContext = remindBean.getRemindContext();
                startAlarm(split[0],split[1],remindBean.getDateStr(),Integer.parseInt(alarmId), remindContext);
            }
            String remindContext = intent.getStringExtra("remindContext");
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
            Notification notify = new Notification.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏中的小图片，尺寸一般建议在24×24
                    .setLargeIcon(bitmap) // 这里也可以设置大图标
                    .setTicker(context.getResources().getString(R.string.app_name)) // 设置显示的提示文字
                    .setContentTitle(remindContext) // 设置显示的标题
                    .setContentText(remindContext) // 消息的详细内容
                    .setNumber(1) // 在TextView的右方显示的数字，可以在外部定义一个变量，点击累加setNumber(count),这时显示的和
                    .getNotification(); // 需要注意build()是在API level16及之后增加的，在API11中可以使用getNotificatin()来
            notify.flags |= Notification.FLAG_AUTO_CANCEL;
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(NOTIFICATION_FLAG, notify);
            bitmap.recycle(); //回收bitmap
        }


    }


    //设置闹钟（在多长时间后闹钟响然后继续计时）
    public void startAlarm(String hour,String minute,String circleTime,int alarmId,String remindContext){
        //获取选择的闹钟时间
        Calendar c=Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
        c.set(Calendar.MINUTE, Integer.parseInt(minute));
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        //给每个闹钟设置不同ID防止覆盖
//        int alarmId = (int) SharedPreferencesUtils.getParam("alarm_id", 0);
//        SharedPreferencesUtils.setParam("alarm_id", alarmId);
        Intent intent=new Intent(BaseApp.getContext(),AlarmReceiver.class);
        intent.setAction("alarm_receiver");
        intent.putExtra("remindContext",remindContext);
        intent.putExtra("alarmId",String.valueOf(alarmId));
        PendingIntent   pendingIntent = PendingIntent.getBroadcast(BaseApp.getContext(), alarmId, intent, 0);
        //实例化闹钟个管理器
        AlarmManager     alarmManager = (AlarmManager) BaseApp.getContext().getSystemService(Context.ALARM_SERVICE);
        //闹钟什么时候继续执行
        long weekCircle = TimeUtils.getWeekCircle(circleTime, c);
        Log.e("MLOG",weekCircle+">>>>>>>>>>>>>>");
        alarmManager.set(AlarmManager.RTC_WAKEUP,weekCircle,pendingIntent);
    }

}

